package ooday03;
/**引用类型数组的演示*/
public class RefArrayDemo {
    public static void main(String[] args) {
        //引用数组的定义：
        //声明Dog型数组，包含3个元素，每个元素都是Dog型，默认值为null
        Dog[] dogs=new Dog[3];
        //声明Chick型数组，包含2个元素，每个元素都是Chick型，默认值为null
        Chick[] chicks=new Chick[2];
        //声明Fish型数组，包含4个元素，每个元素都是Fish型，默认值为null
        Fish[] fish=new Fish[4];
        //引用数组的初始化：给元素赋值，要new一个对象（调用构造方法（））
        dogs[0]=new Dog("旺财",2,"白");
        dogs[1]=new Dog("小黑",3,"黑");
        dogs[2]=new Dog("小黄",1,"黄");
        System.out.println(dogs[0].name);//输出第一只狗狗的名字
        dogs[1].age=4;//修改第2只狗狗的年龄为4岁
        dogs[1].eat();
        dogs[2].swim();
        //遍历
        for(int i=0;i<dogs.length;i++){
            System.out.println(dogs[i].name);//输出每只狗狗的名字
            dogs[i].lookHome();//每只狗狗都在看家
        }
        chicks[0]=new Chick("小鸡",1,"白");
        chicks[1]=new Chick("小乌",2,"黑");
        for(int i=0;i<chicks.length;i++){
            System.out.println(chicks[i].name);//输出每只小鸡的名字
            chicks[i].layEggs();//每只小鸡都在下蛋
        }
        fish[0]=new Fish("小花鱼",1,"黄");
        fish[1]=new Fish("小黄鱼",2,"黄");
        fish[2]=new Fish("小金鱼",3,"金");
        //fish[3].swim();//NullPointerException   空指针异常   没有给第4个元素赋值，所以为null
        fish[3]=new Fish("小红鱼",2,"红");

        for(int i=0;i<fish.length;i++){
            System.out.println(fish[i].name);//输出每只小鱼的名字
            fish[i].swim();//每只小鱼都在游泳
        }


        //遍历动物数组
        Animal[] animals=new Animal[5];
        animals[0]=new Dog("旺财",2,"白");
        animals[1]=new Dog("小黑",3,"黑");
        animals[2]=new Chick("小黄",1,"黄");
        animals[3]=new Chick("小鸡",1,"白");
        animals[4]=new Fish("小花鱼",1,"黄");
        for(int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
        }





    }
}
